using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace IUPUI.StellarResults.Client.UIElements
{
	/// <summary>
	/// A visual representation of the <see cref="Simulation"/> data.
	/// </summary>
	public partial class DataView : UserControl, ISimulationViewTabControl
	{
		#region Private Properties
		private Simulation m_Simulation = null;
		private bool m_PendingPrint;
		private bool m_PendingPrintPreview;
		#endregion Private Properties

		#region Public Properties
		/// <summary>
		/// Gets or sets the simulation.
		/// </summary>
		/// <value>The simulation.</value>
		public Simulation Simulation
		{
			get
			{
				return m_Simulation;
			}
			set
			{
				if (m_Simulation != value)
				{
					m_Simulation = value;
					OnSimulationChanged();
				}
			}
		}
		#endregion Public Properties

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="DataView"/> class.
		/// </summary>
		public DataView()
		{
			InitializeComponent();

			m_DataHtmlControl.ReadyStateComplete += new EventHandler(m_DataHtmlControl_ReadyStateComplete);

			OnSimulationChanged();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataView"/> class.
		/// </summary>
		public DataView(
			IContainer container) : this()
		{
			container.Add(this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataView"/> class.
		/// </summary>
		public DataView(
			IContainer container,
			Simulation simulation)
			: this(container)
		{
			Simulation = simulation;
		}

		#endregion Constructors

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.UserControl.Load"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
		protected override void OnLoad(EventArgs e)
		{
			// Set image to toolstip items
			m_RefreshToolStripButton.Image =
				m_ImageList.Images[m_RefreshAction.ImageIndex];
			m_PrintToolStripButton.Image =
				m_ImageList.Images[m_PrintAction.ImageIndex];
			m_PrintPreviewToolStripButton.Image =
				m_ImageList.Images[m_PrintPreviewAction.ImageIndex];

			base.OnLoad(e);
		}

		#region Public Methods
		/// <summary>
		/// Prints this instance.
		/// </summary>
		public void Print()
		{
			if (m_DataHtmlControl.IsReady)
			{
				m_DataHtmlControl.Print();
			}
			else
			{
				m_PendingPrint = true;
			}
		}

		/// <summary>
		/// Prints the preview.
		/// </summary>
		public void PrintPreview()
		{
			if (m_DataHtmlControl.IsReady)
			{
				m_DataHtmlControl.PrintPreview();
			}
			else
			{
				m_PendingPrintPreview = true;
			}
		}
		#endregion Public Methods

		/// <summary>
		/// Handles the ReadyStateComplete event of the m_DataHtmlControl control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_DataHtmlControl_ReadyStateComplete(object sender, EventArgs e)
		{
			this.Cursor = Cursors.Default;

			if (m_PendingPrint)
			{
				m_DataHtmlControl.Print();
			}
			if (m_PendingPrintPreview)
			{
				m_DataHtmlControl.PrintPreview();
			}
			m_PendingPrint = false;
			m_PendingPrintPreview = false;
		}

		/// <summary>
		/// Called when [simulation changed].
		/// </summary>
		protected virtual void OnSimulationChanged()
		{
			if (m_Simulation != null)
			{
				// Make sure a simulation xslt is specified
				if (String.IsNullOrEmpty(Properties.Settings.Default.SimulationXsltFileName))
				{
					MessageBox.Show(
						this,
						Properties.Resources.SimulationXsltFileNotSpecified,
						Properties.Resources.ErrorTitle,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
					return;
				}

				// Make sure the specified simulation xslt exists
				if (!System.IO.File.Exists(Properties.Settings.Default.SimulationXsltFileName))
				{
					MessageBox.Show(
						this,
						String.Format(
							Properties.Resources.SimulationXsltFileNotFound,
							Properties.Settings.Default.SimulationXsltFileName),
						Properties.Resources.ErrorTitle,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
					return;
				}

				System.IO.StreamReader streamReader = null;
				try
				{
					streamReader = new System.IO.StreamReader(
						Properties.Settings.Default.SimulationXsltFileName);

					this.Cursor = Cursors.WaitCursor;

					// Transform the simulation with the xslt file
					m_DataHtmlControl.LoadHtml(
						ININ.Xml.XsltTransformer.Transform(
							m_Simulation.SaveToString(),
							streamReader.ReadToEnd()));
				}
				catch(Exception ex)
				{
					this.Cursor = Cursors.Default;
					MessageBox.Show(
						this,
						String.Format(
							Properties.Resources.GeneralError,
							System.Environment.NewLine,
							ex.ToString()),
						Properties.Resources.ErrorTitle,
						MessageBoxButtons.OK,
						MessageBoxIcon.Error);
					ININ.Logging.Trace.TraceError(ex.ToString());
				}
				finally
				{
					if (streamReader != null)
					{
						streamReader.Dispose();
						streamReader.Close();
					}
				}
			}
			else
			{
				this.Cursor = Cursors.WaitCursor;
				m_DataHtmlControl.LoadHtml("");
			}
		}

		#region Actions
		/// <summary>
		/// Handles the Execute event of the m_RefreshAction control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_RefreshAction_Execute(object sender, EventArgs e)
		{
			OnSimulationChanged();
		}

		/// <summary>
		/// Handles the Execute event of the m_PrintAction control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_PrintAction_Execute(object sender, EventArgs e)
		{
			Print();
		}

		/// <summary>
		/// Handles the Execute event of the m_PrintPreviewAction control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_PrintPreviewAction_Execute(object sender, EventArgs e)
		{
			PrintPreview();
		}
		#endregion Actions
	}
}
